package com.example.produce;

import com.example.entity.LongEvent;
import com.lmax.disruptor.RingBuffer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.nio.ByteBuffer;

/**
 * @ClassName LongEventProducer
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/7  1:34
 * @Version 1.0
 * <p>
 * 生产者
 */
//@Data
//@AllArgsConstructor
//@NoArgsConstructor
public class LongEventProducer {

    private RingBuffer<LongEvent> ringBuffer;

    public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(ByteBuffer byteBuffer) {
            //获取环形的位置 获取环对应的下标位置。
            long next = ringBuffer.next();
        try {
            //取出来空队列
            LongEvent longEvent = ringBuffer.get(next);
            //给空的队列进行赋值
            longEvent.setValue(byteBuffer.getLong(0));

        } catch (Exception e) {

        } finally {
            System.out.println(" 生产者 发送数据 === ");
            ringBuffer.publish(next);
        }

    }


}
